package com.itheima.service.Impl.course;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Catalog;
import com.itheima.domain.Course;
import com.itheima.mapper.store.catalog.CatalogMapper;
import com.itheima.mapper.store.course.CourseMapper;
import com.itheima.service.CourseService;
import com.itheima.utils.IDutils;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class CourseServiceImp implements CourseService {
    @Override
    public List<Course> findAll(String page, String pageSize) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);

        // 设置默认分页参数
        int page11=1;
        int pageSize22=10;
        // 判断传入的对象是否为空
        if(page!=null){
            page11=Integer.parseInt(page);
        }
        if (pageSize!=null){
            pageSize22=Integer.parseInt(pageSize);
        }
        // 分页助手启动
        PageHelper.startPage(page11,pageSize22);
        // 调用查询方法
        List<Course> list= mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    @Override
    public void save(Course course) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
        course.setCreateTime(new Date());
        course.setId(IDutils.getUUId());
        mapper.save(course);
        session.commit();
        MapperUtil.close(session);

    }

    @Override
    public Course findById(String id) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);
       Course course= mapper.findById(id);


        MapperUtil.close(session);
        return course;
    }

    @Override
    public void update(Course course) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);

        mapper.update(course);

        session.commit();
        MapperUtil.close(session);


    }

    @Override
    public void delete(String id) {
        SqlSession session = MapperUtil.getSession();
        CourseMapper mapper = session.getMapper(CourseMapper.class);

        String[] ids = id.split(",");
        mapper.delete(ids);

        session.commit();
        MapperUtil.close(session);


    }
}
